package chapt02.glrect;
// GLRect.java
// Just draw a single rectangle in the middle of the screen
// OpenGL SuperBible, 3rd Edition
// Richard S. Wright Jr.
// rwright@starstonesoftware.com

import javax.media.opengl.GL;
import javax.media.opengl.GL2;

public class GLRect
{
	// Setup the rendering state
	// Called by GLUT library when the window has changed size
	protected static void setup(GL2 gl2, int w, int h)
	{
		gl2.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
		
		float aspectRatio;
		
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Reset coordinate system
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		// Establish clipping volume (left, right, bottom, top, near, far)
		aspectRatio = (float)w / (float)h;
		if(w <= h)
			gl2.glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
		else
			gl2.glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0);
		
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
	}
	
	// Called to draw scene
	protected static void render(GL2 gl2, int width, int height)
	{
		// Clear the window with current clearing color
		gl2.glClear(GL.GL_COLOR_BUFFER_BIT);
		
		// Set current drawing color to red
		//              R     G     B
		gl2.glColor3f(1.0f, 0.0f, 0.0f);
		
		// Draw a filled rectangle with current color
		gl2.glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
		
		// Flush drawing commands
		gl2.glFlush();
	}
}